package whf.framework.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import whf.framework.log.Log;
import whf.framework.log.LogFactory;

/**
 * @author wanghaifeng
 *
 */
public class EncodingFilter implements Filter {
	private static Log log = LogFactory.getLog(EncodingFilter.class);
	
	private String encode = "UTF-8";

	public void init(FilterConfig config) {
		this.encode = config.getInitParameter("encode");
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain)  throws IOException, ServletException{
		try {
			request.setCharacterEncoding(this.encode);
			response.setContentType("text/html;charset=" + encode);
		} catch (Exception e) {
			log.error(this, e);
		} 
		chain.doFilter(request, response);
	}

	public void destroy() {
	}
}